package com.ygxsk.carhome.ui.member;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.Utils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.response.MemberCardItemBean;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.adapter.MemberCardListAdapter;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-08-02  12:12
 * @Description:会员详情页面内容
 */
public class MemberDetailActivity extends ErrorActivity {


    //顶部点击 更多 的外侧包裹信息
    @BindView(R.id.relativelayoutmoreclick)
    RelativeLayout relativeLayoutMoreClick;

    @BindView(R.id.refreshlayoutinpointdetail)
    SmartRefreshLayout refreshLayoutInPointDetail;

    @BindView(R.id.recyclerviewinpointdetail)
    RecyclerView recyclerViewInPointDetail;

    //会员卡券的列表 信息
    private ArrayList<MemberCardItemBean> mDataResources = new ArrayList();

    //添加适配器
    private MemberCardListAdapter memberCardListAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;


        initloading();

        initViews();


    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_member_detail;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.title_carddetail));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //搜索客户信息，优先关闭软键盘
                KeyboardUtils.hideSoftInput(mActivityInstance);

                methodBack();

            }
        });

        //添加潜在客户的按钮
        toolbarHelper.setLayoutRight(false, 0, "", null);

//        toolbarHelper.setLayoutRight(true, 0, "添加", new ClickUtils.OnDebouncingClickListener() {
//            @Override
//            public void onDebouncingClick(View v) {
//
//                //快速双加的限制，
//                // TODO: 2023-07-11 16:18 提交潜在客户的信息
//                // TODO: 2023-07-11 14:24 跳转到提交潜在客户页面：
//                Bundle extra_bundle = new Bundle();
//                ActivityUtils.startActivity(extra_bundle, LeadAddSubmitActivity.class);
//
//            }
//        });

    }

    @Override
    protected void onClickNodata(View v) {

        gainDatas();

    }

    @Override
    protected void initViews() {

        //单个实体内容
        MemberCardItemBean memberCardItemBean = new MemberCardItemBean();
        memberCardItemBean.setCardName("深化养护品100元抵用券");
        memberCardItemBean.setCardInfo("满200元可用");
        memberCardItemBean.setCardTimeExpir("2028-02-24 10:10:10");
        memberCardItemBean.setCardTypeInfo("折扣券");
        memberCardItemBean.setAmount("1000");
        memberCardItemBean.setCardUnit("元");

        MemberCardItemBean memberCardItemBean1 = new MemberCardItemBean();
        memberCardItemBean1.setCardName("深化养护品100元抵用券");
        memberCardItemBean1.setCardInfo("满200元可用");
        memberCardItemBean1.setCardTimeExpir("2028-02-24 10:10:10");
        memberCardItemBean1.setCardTypeInfo("折扣券");
        memberCardItemBean1.setAmount("5");
        memberCardItemBean1.setCardUnit("折");

        MemberCardItemBean memberCardItemBean2 = new MemberCardItemBean();
        memberCardItemBean2.setCardName("深化养护品100元抵用券");
        memberCardItemBean2.setCardInfo("满200元可用");
        memberCardItemBean2.setCardTimeExpir("2028-02-24 10:10:10");
        memberCardItemBean2.setCardTypeInfo("折扣券");
        memberCardItemBean2.setAmount("100");
        memberCardItemBean2.setCardUnit("元");

        mDataResources.add(memberCardItemBean);
        mDataResources.add(memberCardItemBean1);
        mDataResources.add(memberCardItemBean2);

        //布局的适配器填充，下拉刷新等等；
        recyclerViewInPointDetail.setLayoutManager(new MyLinearLayoutManager(this));
        memberCardListAdapter = new MemberCardListAdapter(R.layout.pointscardsadapter, mDataResources);
        recyclerViewInPointDetail.setAdapter(memberCardListAdapter);

        //头部信息
        memberCardListAdapter.removeAllHeaderView();
        memberCardListAdapter.addHeaderView(getHeaderViewPersonInfo(recyclerViewInPointDetail));
        memberCardListAdapter.addHeaderView(getHeaderViewPointList(recyclerViewInPointDetail));

        memberCardListAdapter.notifyDataSetChanged();

        memberCardListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                // TODO: 2023-07-13 15:07 跳转到这个客户的详情页面
                Bundle extra_bundle = new Bundle();

//                extra_bundle.putParcelable(ExtraKey.EXTRA_RUNMANGROUPMANAGEBEAN, runManGroupManageBeanItem);
//                ActivityUtils.startActivity(extra_bundle, LeadDetailActivity.class);

            }
        });

        //这里有一个下拉刷新的操作,仅仅是一个操作
        refreshLayoutInPointDetail.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                //

            }
        });

    }

    //个人信息的头部信息
    private View getHeaderViewPersonInfo(RecyclerView _recyclerViewWork) {

        View personInfo;
        // java.lang.NullPointerException: Attempt to invoke virtual method
        // 'android.view.ViewParent androidx.recyclerview.widget.RecyclerView.getParent()' on a null object reference
        if ((_recyclerViewWork != null) && (_recyclerViewWork.getParent() != null)) {
            personInfo = LayoutInflater.from(Utils.getApp()).inflate(//
                    R.layout.pointdetailtops_top, //
                    (ViewGroup) _recyclerViewWork.getParent(),//
                    false);//
        } else {
            personInfo = LayoutInflater.from(Utils.getApp()).inflate(R.layout.pointdetailtops_top, null);
        }

        //添加布局内容；是一个点击事件
//        RelativeLayout relativeLayoutTopInPointList = (RelativeLayout) personInfo.findViewById(R.id.relativelayouttopinpointlist);
        //真是的积分的数量
//        TextView textViewpPointMember = (TextView) personInfo.findViewById(R.id.textviewpointmember);

        //单独的适配器填充它；


        //filldatas
        return personInfo;
    }

    private View getHeaderViewPointList(RecyclerView _recyclerViewWork) {

        View assetManageHeader;

        //2021年12月28日14:54:12 发现是崩溃的日志内容：
        // java.lang.NullPointerException: Attempt to invoke virtual method
        // 'android.view.ViewParent androidx.recyclerview.widget.RecyclerView.getParent()' on a null object reference

        if ((_recyclerViewWork != null) && (_recyclerViewWork.getParent() != null)) {
            assetManageHeader = LayoutInflater.from(Utils.getApp()).inflate(//
                    R.layout.pointdetailtops_point_list, //
                    (ViewGroup) _recyclerViewWork.getParent(),//
                    false);//
        } else {
            assetManageHeader = LayoutInflater.from(Utils.getApp()).inflate(R.layout.pointdetailtops_point_list, null);
        }

        //添加布局内容；
        RelativeLayout relativeLayoutTopInPointList = (RelativeLayout) assetManageHeader.findViewById(R.id.relativelayouttopinpointlist);
        //真是的积分的数量
        TextView textViewpPointMember = (TextView) assetManageHeader.findViewById(R.id.textviewpointmember);

        //单独的适配器填充它；


        //filldatas
        return assetManageHeader;
    }


    @Override
    protected void gainDatas() {

        //

    }

    @Override
    protected void methodBack() {
        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDataResources != null && !mDataResources.isEmpty()) {
            mDataResources.clear();
        }

        if (memberCardListAdapter != null) {
            memberCardListAdapter.notifyDataSetChanged();
            memberCardListAdapter = null;
        }
    }
}